//
//  ZBTextView.swift
//  Wanke
//
//  Created by BO ZHOU on 2020/10/7.
//

import UIKit

open class ZBTextView: UIView {
    
    /** 文本框输入内容变化回调 */
    var textViewDidChangedBlock: ((_ contentString: String)->Void)?
    

    /** 背景色 */
    var bgColor: UIColor!
    
    /** content上下左右边距 */
    var contentInset: UIEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    
    /** 真实输入的字体颜色, 字体大小 */
    var contentColor: UIColor = .white
    var contentFont: UIFont = UIFont.systemFont(ofSize: 15)
    
    /** 光标颜色 */
    var contentTintColor: UIColor = .white
    
    /** placeHolder */
    var placeHolder: String = "请输入..."
    
    var placeHolderColor: UIColor = UIColor.white.withAlphaComponent(0.5)
    
    var placeHolderFont: UIFont = UIFont.systemFont(ofSize: 14)
    
    /** 统计label的一些属性 */
    var countingLabelColor: UIColor = UIColor.white.withAlphaComponent(0.8)
    var countingLabelFont: UIFont = UIFont.systemFont(ofSize: 12)
    
    /** countingLabel显示的format */
    var countingLabelFormat: String = "%d / %d"
    
    /** 统计信息label最大可输入内容长度 */
    var contentMaxCount: Int = 500
    
    /** 当前已经输入的内容长度 */
    var currentContentCount: Int = 0 {
        didSet {
            countingLabel.text = String(format: countingLabelFormat, currentContentCount, contentMaxCount)
            countingLabel.sizeToFit()
            countingLabel.setNeedsLayout()
        }
    }
    
    /** placeHolderLabel */
    lazy var placeHolderLabel: UILabel = {
        let label = UILabel()
        label.text = placeHolder
        label.textColor = placeHolderColor
        label.font = placeHolderFont
        label.numberOfLines = 0
        
        return label
    }()
    
    /** textView */
    lazy var textview: UITextView = {
        let view = UITextView()
        view.font = contentFont
        view.textColor = contentColor
        view.tintColor = contentTintColor
        view.delegate = self
        
        view.textContainer.lineFragmentPadding = 0
        view.textContainerInset = .zero
        
        return view
    }()

    /** 右下角统计文字label */
    lazy var countingLabel: UILabel = {
        let label = UILabel()
        label.text = String(format: countingLabelFormat, 0, contentMaxCount)
        label.font = countingLabelFont
        label.textColor = countingLabelColor
        
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setup()
    }
    
    required public init?(coder: NSCoder) {
        super.init(coder: coder)
        
        setup()
    }
    
    private func setup() {
        
        addSubview(textview)
        addSubview(placeHolderLabel)
        addSubview(countingLabel)
        
        textview.backgroundColor = .clear
        self.backgroundColor = .clear
        
        textview.backgroundColor = UIColor.clear
        self.backgroundColor = UIColor.white.withAlphaComponent(0.06)
    }
    
    public override func layoutSubviews() {
        super.layoutSubviews()
        
        textview.frame = CGRect(x: contentInset.left, y: contentInset.top, width: self.width - contentInset.left - contentInset.right, height: self.height - contentInset.top - contentInset.bottom)

        placeHolderLabel.frame = CGRect(x: textview.x, y: textview.y-3, width: textview.width, height: 24)
        
        countingLabel.sizeToFit()
        countingLabel.origin = CGPoint(x: self.width - contentInset.right - countingLabel.width, y: self.height - contentInset.bottom - countingLabel.height)
    }
    
}


extension ZBTextView: UITextViewDelegate {
    
    public func textViewDidChange(_ textView: UITextView) {
        
        if textView.markedTextRange == nil {
            
            if textView.text.count == 0 {
                self.placeHolderLabel.isHidden = false
            } else {
                self.placeHolderLabel.isHidden = true
            }
            
            if textView.text.count > contentMaxCount {
                textView.text = textView.text.ns_string.substring(to: contentMaxCount - 1)
            }
            currentContentCount = textView.text.count
            
            textViewDidChangedBlock?(textView.text)
        }

    }
}
